<template>
  <div class="container">
    <div class="header">
      <el-input
        v-model="input"
        placeholder="请输入菜品名称"
        clearable
        @input="onInputChange"
      >
        <i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="init"
        ></i>
      </el-input>
      <div class="rightmenu">
        <div class="link" :underline="false">
          <el-link type="danger" :underline="false" @click="btnBatchDel()"
            >批量删除</el-link
          >
          <el-divider direction="vertical"></el-divider>
          <el-link type="primary" :underline="false" @click="btnBatchChange(1)"
            >批量启售</el-link
          >
          <el-divider direction="vertical"></el-divider>
          <el-link type="danger" :underline="false" @click="btnBatchChange(0)"
            >批量停售</el-link
          >
        </div>
        <el-button icon="el-icon-plus" @click="addDish()">新建菜品</el-button>
      </div>
    </div>

    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column prop="name" label="菜品名称" align="center">
      </el-table-column>
      <el-table-column prop="image" label="图片" align="center">
        <template slot-scope="row">
          <img
            :src="baseApi + '/common/download?name=' + row.row.image"
            style="width: 40px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类" align="center">
      </el-table-column>
      <el-table-column label="售价" width="120" align="center">
        <template v-slot="row"> ￥{{ row.row.price }} </template>
      </el-table-column>
      <el-table-column label="售卖状态" align="center">
        <template v-slot="row">
          {{ row.row.status ? '启售' : '停售' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="最后操作时间"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip align="center">
        <template v-slot="{ row }">
          <el-link type="primary" :underline="false" @click="editDish(row.id)"
            >修改</el-link
          >
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该菜品的售卖状态?"
            @onConfirm="handleChange()"
          >
            <el-link
              type="primary"
              slot="reference"
              :underline="false"
              @click="btnChange(row)"
              >{{ row.status ? '停售' : '启售' }}</el-link
            >
          </el-popconfirm>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认删除该菜品?" @onConfirm="handleDel()">
            <el-link
              type="danger"
              :underline="false"
              slot="reference"
              @click="btnDel(row)"
              >删除</el-link
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getCuisine, changeStatus, dishDel } from '@/api/dish'
import debounce from 'lodash/debounce'

export default {
  name: 'CuisineIndex',
  data () {
    return {
      input: '',
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
      debouncedInit: null,
      multipleSelection: [],
      params: { status: '', ids: [] }
    }
  },
  created () {
    this.debouncedInit = debounce(this.init, 300)
    this.init()
  },
  computed: {
    baseApi () {
      return process.env.VUE_APP_BASE_API
    }
  },

  methods: {
    init () {
      getCuisine({ ...this.pageInfo, name: this.input }).then(res => {
        const { records, total } = res.data
        this.tableData = records
        this.total = total
      })
    },
    handleSizeChange (val) {
      this.pageInfo.page = 1
      this.pageInfo.pageSize = val
      this.init()
    },
    handleCurrentChange (val) {
      this.pageInfo.page = val
      this.init()
    },
    onInputChange () {
      this.debouncedInit()
    },
    addDish () {
      this.$router.push({ path: '/cuisine/add' })
    },
    editDish (id) {
      this.$router.push({ path: '/cuisine/edit', query: { id } })
    },

    async handleChange () {
      const res = await changeStatus(this.params)
      if (res.code === 1) {
        this.$message.success('菜品状态已经更改成功！')
        this.init()
      } else {
        this.$message.error('菜品状态更改失败')
      }
    },
    // 改变单个售卖状态
    btnChange (row) {
      this.params = { status: row.status === 0 ? 1 : 0, ids: row.id.split() }
    },
    // 批量改变销售状态
    btnBatchChange (status) {
      if (!this.multipleSelection.length) {
        this.$message.error('请选择菜品')
      } else {
        this.$confirm('确认批量更改状态？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.params = {
            status: status,
            ids: this.multipleSelection.map(item => item.id)
          }
          this.handleChange(this.params)
        })
      }
    },
    async handleDel () {
      const res = await dishDel(this.params)
      if (res.code === 1) {
        this.$message.success('菜品删除成功！')
        this.init()
      } else {
        this.$message.error('菜品删除失败')
      }
    },
    // 删除单个菜品
    btnDel (row) {
      this.params = { ids: row.id.split() }
      console.log(this.params)
    },
    // 批量删除菜品
    btnBatchDel () {
      if (!this.multipleSelection.length) {
        this.$message.error('请选择删除对象')
      } else {
        this.$confirm('确认批量删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.params = {
            ids: this.multipleSelection.map(item => item.id)
          }
          this.handleDel(this.params)
        })
      }
    },
    // 确认更改状态
    handleSelectionChange (val) {
      this.multipleSelection = val
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

.container {
  padding: 30px;
  .header {
    display: flex;
    justify-content: space-between;
    .rightmenu {
      display: flex;
      justify-content: space-between;
      .link {
        font-size: 14px;
        padding: 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .el-link {
          padding: 0 20px;
        }
      }
    }
    .el-button {
      background-color: $darkorange;
      color: #333;
      &:hover {
        background-color: $lightorange;
      }
    }
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
  }
  .el-table {
    margin-top: 20px;
    ::v-deep .el-table__header-wrapper {
      .el-checkbox__input.is-checked {
        .el-checkbox__inner {
          background-color: $darkorange;
          border-color: $darkorange;
        }
      }
    }
    ::v-deep .el-table__body-wrapper {
      .el-table__body {
        .el-table__row {
          .el-checkbox__input.is-checked {
            .el-checkbox__inner {
              background-color: $darkorange;
              border-color: $darkorange;
            }
          }
        }
      }
    }
  }
  .footer {
    margin: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>
